package com.example.njupt.views.toolbar;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import com.example.njupt.R;

/**
 * Author:      mwang
 * Create Date: Created in 2021/4/23 17:17
 * Update Date:
 * Modified By:
 * Description:
 */
public class ToolbarHelper {

    private Toolbar mToolbar;
    private Context context;

    private ConstraintLayout mBaseContainer;
    private LinearLayout mLeftContainer;
    private LinearLayout mRightContainer;
    private TextView mTitle;
    private ImageView ivBack;
    private View mStatusBarMarginView;

    public ToolbarHelper(Context context, Toolbar toolbar) {
        this.context = context;
        mToolbar = toolbar;
        if (mToolbar == null) {
            throw new IllegalArgumentException("Layout file is required to include a Toolbar with id: toolbar");
        }

        mBaseContainer = mToolbar.findViewById(R.id.toolbar_base_container);
        mLeftContainer = mToolbar.findViewById(R.id.toolbar_left_container);
        mRightContainer = mToolbar.findViewById(R.id.toolbar_right_container);
        mTitle = mToolbar.findViewById(R.id.toolbar_title);
        ivBack = mToolbar.findViewById(R.id.iv_back);
        mStatusBarMarginView = mToolbar.findViewById(R.id.status_bar_margin_view);
    }

    public void enableTransparentToolbar() {
        mToolbar.setBackgroundResource(android.R.color.transparent);
    }

    public void setBackgroundRes(@DrawableRes int resId) {
        mToolbar.setBackgroundResource(resId);
    }

    public void setBackgroundColor(@ColorRes int color) {
        mToolbar.setBackgroundColor(mToolbar.getContext().getColor(color));
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }

    public void setTitle(@StringRes int resId) {
        if (null != mTitle) {
            mTitle.setText(resId);
        }
    }

    public void setTitle(CharSequence charSequence) {
        if (null != mTitle) {
            mTitle.setText(charSequence);
        }
    }

    public void setTitleTextColor(@ColorRes int color) {
        if (null != mTitle) {
            mTitle.setTextColor(mToolbar.getContext().getColor(color));
        }
    }
    /*----------------------------------------------------- divider ------------------------------------------------------*/

    public void ableBack(final Activity activity) {
        ivBack.setOnClickListener(v -> activity.onBackPressed());
        mLeftContainer.setOnClickListener(v -> activity.onBackPressed());
    }

    public void enableBack(){
        ivBack.setVisibility(View.GONE);
    }
    /**
     * 在Toolbar与状态栏之间绘制一个空View,高度与状态栏相同，以达到排除状态栏侵入
     *
     * @param immersive 是否侵入
     */
    public void setImmersive(boolean immersive) {
        if (!immersive) {
            int statusBarHeightRes = getToolbar().getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (statusBarHeightRes > 0) {
                //根据资源ID获取响应的尺寸值
                int statusBarHeight = getToolbar().getResources().getDimensionPixelSize(statusBarHeightRes);
                ConstraintSet set = new ConstraintSet();
                set.clone(mBaseContainer);
                set.constrainHeight(R.id.status_bar_margin_view, statusBarHeight);
                set.applyTo(mBaseContainer);
            }
        }
    }

}
